<template>
  <div class="login">
    <h1>登录页</h1>
    <el-form ref="form" :model="loginForm" label-width="120px" :rules="rules">
      <el-form-item label="请输入用户名" prop="name">
        <el-input v-model="loginForm.name" ></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input  
          type="password"
          v-model="loginForm.password"
          autocomplete="off"
          show-password
        ></el-input>
      </el-form-item>
      <el-button type="primary" @click="toLogin">登录</el-button>
    </el-form>

  </div>
</template>

<script setup>
import { ElInput, ElForm, ElFormItem, ElButton } from 'element-plus'
import { reactive, getCurrentInstance } from 'vue'

import { useRouter} from 'vue-router'
import { login } from '@/api/user'
import { useStore } from 'vuex'
console.log(import.meta.env)
    console.log(import.meta.env.MODE)
let router = useRouter()
const {proxy} = getCurrentInstance()
const store = useStore()

var loginForm = reactive({
  name:'admin',
  password:'admin'
})
let rules = {
  name:[{ required: true, message: '请输入活动名称', trigger: 'blur' }],
  password:[{ required: true, message: '请输入密码', trigger: 'blur' }]
}
var toLogin = function(){
  proxy.$refs['form'].validate((valid) => {
    if (valid) {
      login(loginForm).then(res=>{
        console.log(res)
        if(res.code==200){
          store.commit("setUser",res.data.user)
          store.commit("setToken",res.data.token)
          router.push('/index')
        }
      })
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
</script>

<style lang="scss" scoped>
.login{
  width: 400px;
  margin: 100px auto;
  text-align: center;
}
</style>